package com.kkd.kkdapp.adapter;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.activity.MyDiscountCouponActivity;
import com.kkd.kkdapp.base.MyBaseAdapter;
import com.kkd.kkdapp.http.entity.DiscountCoupon;
import com.kkd.kkdapp.utils.LogUtil;

public class MyDiscountCouponGridAdapter extends MyBaseAdapter<DiscountCoupon> {

	private List<DiscountCoupon> sList;
	MyDiscountCouponActivity myDiscountCouponActivity;
	private int mPosition = 0;

	public MyDiscountCouponGridAdapter(Context context) {
		super(context);
		sList = this.getAdapterData();
	}

	@Override
	public View getMyView(int position, View convertView, ViewGroup parent) {
		ViewHolder mViewHolder;
		if (convertView == null) {
			convertView = View.inflate(context, R.layout.mycoupongridview_item,
					null);
			mViewHolder = new ViewHolder();
			// mViewHolder.icon_img = (ImageView) convertView
			// .findViewById(R.id.icon_img);
			mViewHolder.icon_tv = (TextView) convertView
					.findViewById(R.id.icon_tv);
			mViewHolder.bg_tv = (TextView) convertView.findViewById(R.id.bg_tv);
			convertView.setTag(mViewHolder);
		} else {
			mViewHolder = (ViewHolder) convertView.getTag();
		}
		
		mViewHolder.bg_tv.setBackgroundResource(R.color.violet_ddd8ec);
		if (mPosition != position) {
			mViewHolder.bg_tv.setBackgroundResource(R.color.white);
		}
		mViewHolder.icon_tv.setText(sList.get(position).getCouponName());
		// if ("期待中".equals(sList.get(position).getCouponName())) {
		// mViewHolder.icon_tv.setTextColor(context.getResources().getColor(
		// R.color.gray_999999));
		// mViewHolder.icon_img.setVisibility(View.GONE);
		// } else {
		// mViewHolder.icon_img.setVisibility(View.VISIBLE);
		// mViewHolder.icon_img.setImageResource(sList.get(position)
		// .getCouponimg());
		// }
		convertView.setOnClickListener(new GridViewOnClickListener(position,
				mViewHolder.bg_tv));
		return convertView;
	}

	class ViewHolder {

		ImageView icon_img;
		TextView icon_tv;
		TextView bg_tv;

	}

	class GridViewOnClickListener implements OnClickListener {

		private int position;
		TextView tv;

		public GridViewOnClickListener(int position, TextView tv) {
			this.position = position;
			this.tv = tv;
		}

		@Override
		public void onClick(View v) {
			LogUtil.e("position", "" + position);
			if (mOnGridViewItemCliclistener != null) {
				mOnGridViewItemCliclistener.onPosition(position);
				tv.setBackgroundResource(R.color.violet_ddd8ec);
				mPosition = position;
			}
		}
	}

	OnGridViewItemCliclistener mOnGridViewItemCliclistener;

	public void setOnGridViewItemCliclistener(
			OnGridViewItemCliclistener callback) {
		this.mOnGridViewItemCliclistener = callback;
	};

	public interface OnGridViewItemCliclistener {
		public void onPosition(int poi);
	}

}
